package com.hm.service.impl;

import com.hm.mapper.UserMapper;
import com.hm.pojo.User;
import com.hm.service.UserService;
import com.hm.utils.Md5Util;
import com.hm.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User findUserByUsername(String username) {
        return userMapper.findUserByUsername(username);
    }

    @Override
    public void insertUser(String username, String password) {
        // 把密码加密一下
        password = Md5Util.getMD5String(password);
        userMapper.insertUser(username, password);
    }

    /**
     * 更新用户信息
     * @param user
     */
    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void updateAvata(String avatar) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvata(avatar,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(newPwd,id);
    }
}
